<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\Admin;

use App\Controller\AppBaseController;
use App\Model\CompanyInfo;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\AdminApiAuth;

#[Controller(prefix: 'admin')]
#[Middleware(AdminApiAuth::class)]
class CompanyInfoController extends AppBaseController
{

    /**
     * 后台--企业信息|详情
     * @return object
     */
    #[RequestMapping(path:'companyInfo',methods: 'get')]
    public function companyInfo(): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $info = CompanyInfo::where('id',1)->first();
            if(empty($info)) return $this->reMsg->re(10008);

            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }

    /**
     * 后台--企业信息|编辑
     * @return object
     */
    #[RequestMapping(path: 'companyInfoEdit',methods: 'post,get')]
    public function companyInfoEdit(): object
    {
        try{
            //验证管理员信息
            $checkInfo = $this->checkAdmin();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_admin = $checkInfo['data'];

            $data = $this->request->all();
            //虚拟数据
//            if(empty($data)){
//                $data['title'] = '1231312';
//                $data['photo'] = '/fqxinyi23/test/xyMessage1689216632369.png';
//                $data['content'] = '1111111';
//                $data['link_url'] = 'https://www.baidu.com';
//            }
            //虚拟数据

            if(empty($data['title'])
               || empty($data['photo'])
            ){
                return $this->reMsg->re(10007);
            }

            if(empty($data['content'])) $data['content'] = null;
            if(empty($data['link_url'])) $data['link_url'] = null;

            //编辑
            $info = CompanyInfo::where('id',1)->first();
            if(empty($info)) return $this->reMsg->re(10008);
            if($_admin->level!=1) return $this->reMsg->re(10009);
            $info->title = $data['title'];
            $info->photo = $data['photo'];
            $info->content = $data['content'];
            $info->link_url = $data['link_url'];
            $info->admin_id = $_admin->id;
            $info->save();
            return $this->reMsg->re(1);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


}